<?php

namespace app\model\system;

use app\model\BaseModel;
use albert\traits\ModelTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 系统角色 Model
 * Class SystemRole
 * @package app\model\system
 */
class SystemRole extends BaseModel
{
    use ModelTrait;

    /**
     * 权限字段 修改器
     * Author: Albert3306
     * Date: 2021/4/26
     * @param $value
     * @return string
     */
    public static function setRulesAttr($value): string
    {
        return is_array($value) ? implode(',',$value) : $value;
    }

    /**
     * 初始化查询条件
     * Author: Albert3306
     * Date: 2021/5/1
     * @param $where
     * @param null $model
     * @return SystemRole|mixed
     */
    public static function initWhere($where, $model = null)
    {
        $model = $model ?? new self();
        if (isset($where['ids']) && $where['ids'] !== '') {
            $model = $model->whereIn('id', $where['ids']);
        }
        if (isset($where['status']) && $where['status'] !== '') {
            $model = $model->where('status', $where['status']);
        }
        if (isset($where['keywords']) && $where['keywords'] !== '') {
            $model = $model->whereLike('name', '%' . $where['keywords'] . '%');
        }
        return $model;
    }

    /**
     * 获取所有权限
     * Author: Albert3306
     * Date: 2021/4/26
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getAllAuth(): array
    {
        static $auth = null;
        $auth === null  && ($auth = self::tidyAuth(SystemMenus::all(function($query){
            $query->where('controller|action','<>','')->field('id,module,controller,action,params');
        })?:[]));
        return $auth;
    }

    /**
     * 权限规则整理
     * Author: Albert3306
     * Date: 2021/4/26
     * @param $_auth
     * @return array
     */
    protected static function tidyAuth($_auth): array
    {
        $auth = [];
        foreach ($_auth as $val){
            $auth[$val['id']] =  SystemMenus::getAuthName($val['action'],$val['controller'],$val['module'],$val['params']);
        }
        return $auth;
    }

    /**
     * 获取角色权限
     * Author: Albert3306
     * Date: 2021/4/26
     * @param $rules
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function rolesByAuth($rules): array
    {
        if(empty($rules)) return [];
        $rules = self::initWhere(['ids' => $rules])->where('status', '1')->column('rules', 'id');
        $rules = array_unique(explode(',',implode(',', $rules)));
        $_auth = SystemMenus::all(function($query) use($rules){
            $query->where('id','IN',$rules)
                ->where('controller|action','<>','')
                ->field('id,module,controller,action,params');
        });
        return self::tidyAuth($_auth?:[]);
    }

    /**
     * 获取 LayUI 列表数据
     * Author: Albert3306
     * Date: 2021/5/1
     * @param $params
     * @param string $field
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getLayUIPageList($params, string $field = '*'): array
    {
        $model = self::initWhere($params);
        $count = $model->count();
        $data = $model->field($field)->page((int)$params['page'], (int)$params['limit'])->select()->toArray();
        return compact('count', 'data');
    }

    /**
     * 选择管理员身份
     * Author: Albert3306
     * Date: 2021/5/7
     * @param int $level
     * @return array
     */
    public static function getRole(int $level = 0): array
    {
        return self::initWhere(['status' => 1, 'level' => $level])->column('name','id');
    }
}